#include"time.h"
#include"graph.h"
#include<ctime>

void PassingCrossroad(Time& t, Crossroad &cro, int quadrant) {       //经过十字路口
	if (quadrant == 1) {   //第一象限
		int i = 0;
		for (; i < cro.GetGreen1().size(); i++) {
			if (t >= (cro.GetGreen1())[i].begin) {
				break;
			}
		}
		if (t <= (cro.GetGreen1())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro.GetGreen1())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 2) {   //第二象限
		int i = 0;
		for (; i < cro.GetGreen2().size(); i++) {
			if (t >= (cro.GetGreen2())[i].begin) {
				break;
			}
		}
		if (t <= (cro.GetGreen2())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro.GetGreen2())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 3) {   //第三象限
		int i = 0;
		for (; i < cro.GetGreen3().size(); i++) {
			if (t >= (cro.GetGreen3())[i].begin) {
				break;
			}
		}
		if (t <= (cro.GetGreen3())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro.GetGreen3())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 4) {   //第四象限
		int i = 0;
		for (; i < cro.GetGreen4().size(); i++) {
			if (t >= (cro.GetGreen4())[i].begin) {
				break;
			}
		}
		if (t <= (cro.GetGreen4())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro.GetGreen4())[i + 1].begin;
			return;    //需要等红灯
		}
	}
}

void PassingCrossroad(Time& t, Dot* cro, int quadrant) {       //经过十字路口
	if (quadrant == 1) {   //第一象限
		int i = 0;
		for (; i < cro->GetGreen1().size(); i++) {
			if (t >= (cro->GetGreen1())[i].begin) {
				break;
			}
		}
		if (t <= (cro->GetGreen1())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro->GetGreen1())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 2) {   //第二象限
		int i = 0;
		for (; i < cro->GetGreen2().size(); i++) {
			if (t >= (cro->GetGreen2())[i].begin) {
				break;
			}
		}
		if (t <= (cro->GetGreen2())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro->GetGreen2())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 3) {   //第三象限
		int i = 0;
		for (; i < cro->GetGreen3().size(); i++) {
			if (t >= (cro->GetGreen3())[i].begin) {
				break;
			}
		}
		if (t <= (cro->GetGreen3())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro->GetGreen3())[i + 1].begin;
			return;    //需要等红灯
		}
	}
	if (quadrant == 4) {   //第四象限
		int i = 0;
		for (; i < cro->GetGreen4().size(); i++) {
			if (t >= (cro->GetGreen4())[i].begin) {
				break;
			}
		}
		if (t <= (cro->GetGreen4())[i].end) {
			return;    //绿灯
		}
		else {
			t = (cro->GetGreen4())[i + 1].begin;
			return;    //需要等红灯
		}
	}
}

Time GetSystemTime() {    //获取本地时间
	struct tm t;   //tm结构指针
	time_t now;  //声明time_t类型变量  
	time(&now);      //获取系统日期和时间
	localtime_s(&t, &now);   //获取当地日期和时间
	int h = t.tm_hour;
	int m = t.tm_min;
	int s = t.tm_sec;
	Time* p = new Time(h,m,s);
	return *p;
}

Time Tran(int a) {    //以秒为单位，把int类型对象转为时间类型
	int h = a / 3600;
	a -= h * 3600;
	int m = a / 60;
	a -= m * 60;
	int s = a;
	Time* p = new Time(h, m, s);
	return *p;
}

int Determinquadrant(Dot* Cro,Line *NextRoad) {   //确定经过十字路口方向对应的象限
	double x = Cro->Getx();
	double y = Cro->Gety();
	//先定道路向右还是向左
	if (NextRoad->GetEdge1ID() == DotSearch[Cro]) {   //道路方向向右
		if (NextRoad->Gettype() == 1) {
			if (NextRoad->GetSlope() >= 0) {
				return 1;   //第一象限
			}
			else {
				return 4;   //第四象限
			}
		}
		else {
			//曲线功能扩展完成后在此补充代码
		}
	}
	if (NextRoad->GetEdge2ID() == DotSearch[Cro]) {   //道路方向向左
		if (NextRoad->Gettype() == 1) {
			if (NextRoad->GetSlope() >= 0) {
				return 3;   //第三象限
			}
			else {
				return 2;   //第二象限
			}
		}
		else {
			//曲线功能扩展完成后在此补充代码
		}
	}
}

void CalculateRootTime(vector<Dot*> path, Time& ArriveTime, Time& ProcessTime, double speed,int &CrossroadSum) {            //计算路径时间，用引用传递方式获取到达时间和花费时间，以及所经过的十字路口的个数,要传入对应的速度，最后一个参数传0
	ArriveTime = GetSystemTime();
	Time StartTime= GetSystemTime();
	for (int i = 0; i < path.size() - 1; i++) {          
		if (i != 0 &&i!=path.size() - 1&&path[i]->Gettype() == 3) {    //经过十字路口花费时间
			CrossroadSum++;
			Line* p = NULL;
			for (int r = 0; r < LineKu.size(); r++) {      
				if (LineKu[r]->GetEdge1ID() == DotSearch[path[i]] && LineKu[r]->GetEdge2ID() == DotSearch[path[i + 1]] || LineKu[r]->GetEdge2ID() == DotSearch[path[i]] && LineKu[r]->GetEdge1ID() == DotSearch[path[i + 1]]) {   //找到对应的线
					p = LineKu[r];
					break;
				}
			}
			int quadrant = Determinquadrant(path[i], p);
			PassingCrossroad(ArriveTime, path[i], quadrant);
		}
		

		for (int r = 0; r < LineKu.size(); r++) {      //道路花费时间
			if (LineKu[r]->GetEdge1ID() == path[i]->GetID() && LineKu[r]->GetEdge2ID() == path[i+1]->GetID() || LineKu[r]->GetEdge2ID() == path[i]->GetID() && LineKu[r]->GetEdge1ID() == path[i+1]->GetID()) {   //找到对应的线
				ArriveTime = ArriveTime + Tran(LineKu[r]->Getlength() / speed);
				break;
			}
		}
	}
	ProcessTime = ArriveTime - StartTime;
	return;
}

/*调用说明
Time *ArriveTime=new Time(0,0,0);
Time *ProcessTime=new Time(0,0,0);
int CrossroadSum=0;
CalculateRootTime(path,  *ArriveTime, *ProcessTime, speed，CrossroadSum)
*/